1949C - Annual Ants' Gathering - CodeForces Solution


dfs and similar dp greedy trees

Please click on ads to support us..

Python Code:

from sys import stdin
input = lambda: stdin.readline().rstrip('\r\n')
from heapq import heappush, heappop
 
 
n = int(input())
adj = [[] for _ in range(n+1)]
deg = [0]*(n+1)
 
for _ in range(n-1):
    u, v = map(int, input().split())
    adj[u] += [v]; deg[u] += 1
    adj[v] += [u]; deg[v] += 1
 
pq = [(1, u) for u in range(1, n+1) if deg[u]==1]
sz = [1]*(n+1)
 
while pq:
    s, u = heappop(pq)
    if deg[u] <= 0: continue
    for v in adj[u]:
        if sz[v] >= sz[u]:
            deg[v] -= 1
            sz[v] += sz[u]
            if deg[v] == 1:
                heappush(pq, (sz[v], v))
 
print('YES' if n in sz else 'NO')


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses